/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doancnpm1;

import connect.DBUtil;
import java.awt.Dimension;
import java.awt.Graphics;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author soul-lyno
 */
public class Search extends javax.swing.JFrame {
    DefaultTableModel model;
    /**
     * Creates new form Search
     */
    public Search() {
        initComponents();
        loadTenMonAn();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel(){
            ImageIcon icon = new ImageIcon("src/doancnpm1/images/5.jpg");
            public void paintComponent(Graphics g){
                Dimension d = getSize();
                g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
                setOpaque(false);
                super.paintComponent(g);}
        };
        jScrollPane1 = new javax.swing.JScrollPane();
        tab_search = new javax.swing.JTable();
        cmd_timkiem = new javax.swing.JButton();
        cb_luachon = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtTimKiem = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tab_search.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Món Ăn", "Tên Món Ăn", "THiết Bị", "Nguyên Liệu", "Quy Trình"
            }
        ));
        jScrollPane1.setViewportView(tab_search);

        cmd_timkiem.setText("Serach");
        cmd_timkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_timkiemActionPerformed(evt);
            }
        });

        cb_luachon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MaMonAn", "TenMonAn", " " }));

        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setText("Filtered by: ");

        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("Tìm Kiếm");

        jButton1.setText("ĐÓNG");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(72, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(45, 45, 45)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cb_luachon, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(34, 34, 34)
                            .addComponent(cmd_timkiem))))
                .addGap(52, 52, 52))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(cb_luachon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmd_timkiem))
                .addGap(42, 42, 42)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(jButton1)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public Vector tenMonAn(){
        //Vector v = new Vector();
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        //String maMonAn = txtTimKiem.getText();
        String sqlComBoBox = "Select TenMonAn from MonAn";
        Vector v = new Vector();
        v.addElement("Select");
        //String sQL = "Select * from MonAn where TenMonAn = '" + TenMonAn + "'";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sqlComBoBox);
            
            while(rs.next()){
                v.addElement(rs.getString(1));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } 
        return v;
        
    }
    
    public void loadTenMonAn(){
        Vector v = new Vector();
        v = tenMonAn();
        cb_luachon.setModel(new DefaultComboBoxModel(v));
    }
    private void cmd_timkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_timkiemActionPerformed
        // TODO add your handling code here:
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        String maMonAn = txtTimKiem.getText();
        String tenMonAn = (String) cb_luachon.getSelectedItem();
        if (maMonAn.equals("")) {
            if (cb_luachon.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(null, "moi Ban nhap can tim");
            } else {
                 String sQL = "Select * from MonAn where TenMonAn = '" + tenMonAn + "'";
        //String sQL = "Select * from MonAn where TenMonAn = '" + TenMonAn + "'";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sQL);
            model=(DefaultTableModel)tab_search.getModel();
            while(rs.next()){
                Vector v = new Vector();
                v.addElement(rs.getString(1));
                v.addElement(rs.getString(2));
                v.addElement(rs.getString(3));
                v.addElement(rs.getString(4));
                v.addElement(rs.getString(5));
                model.addRow(v);
            }
            rs.close();
            stmt.close(); 
            
          
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Khong tim thay");
            //Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
           
        } 
            }
        } else {
             String sQL = "Select * from MonAn where MaMonAn = '" + maMonAn + "'";
        //String sQL = "Select * from MonAn where TenMonAn = '" + TenMonAn + "'";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sQL);
            model=(DefaultTableModel)tab_search.getModel();
            while(rs.next()){
                Vector v = new Vector();
                v.addElement(rs.getString(1));
                v.addElement(rs.getString(2));
                v.addElement(rs.getString(3));
                v.addElement(rs.getString(4));
                v.addElement(rs.getString(5));
                model.addRow(v);
            }
            rs.close();
            stmt.close(); 
             Clear();
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Khong tim thay");
            //Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
           
        } 
        }
        
        
    }//GEN-LAST:event_cmd_timkiemActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        // DONGS GIAO DIỆN
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Search.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Search total = new Search();
                total.setLocationRelativeTo(total);
                total.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_luachon;
    private javax.swing.JButton cmd_timkiem;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tab_search;
    private javax.swing.JTextField txtTimKiem;
    // End of variables declaration//GEN-END:variables

    private void Clear() {
        txtTimKiem.setText("");
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
